<%@ Page Language="C#" %>
<%@ Import Namespace="BLF.WWW_WWW.Wiki" %>
<%@ Import Namespace="Model.WWW_WWW.Wiki" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
        private int size = 96;
		private long quality = 80; // Options should be: 40/80/100

		protected void Page_Load(object sender, EventArgs e) 
		{

            if (Request["Image"] == null) Response.Redirect("/knowledge/Default.htm");

			string image = Settings.UploadDirectory + Request["Image"];
            if (!File.Exists(image)) Response.Redirect("/knowledge/Default.htm");

			bool big = false;

			if(Request["Big"] != null) {
				size = 248;
				big = true;
			}

			string thumbFile = Tools.GenerateImageNameHash(Request["Image"], big) + ".jpg";

			if(!File.Exists(Settings.TempDirectory + thumbFile)) {

				FileInfo fi = new FileInfo(image);

				Bitmap bmp = new Bitmap(image);

				Bitmap result = new Bitmap(size, size, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

				Graphics gr = Graphics.FromImage(result);
				// High quality interpolation and text AA to improve appearance
				if(quality >= 75) {
					gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
				}
				gr.SmoothingMode = SmoothingMode.AntiAlias;
				//gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // This is not used because a Path is used to render the text
				gr.Clear(Color.White);

				int width, height;

				float ratio = (float)bmp.Width / (float)bmp.Height;
				if(bmp.Width > bmp.Height) {
					// Landscape Image
					if(bmp.Width < size) {
						// Center the image without resizing it
						width = bmp.Width;
					}
					else {
						// Fit to size
						width = size;
					}
					height = (int)((float)width / ratio);
				}
				else if(bmp.Height > bmp.Width) {
					// Portrait Image
					if(bmp.Height < size) {
						// Center the image without resizing it
						height = bmp.Height;
					}
					else {
						// Fit to size
						height = size;
					}
					width = (int)((float)height * ratio);
				}
				else {
					// Square Image
					if(bmp.Width < size) {
						// Center the image
						width = bmp.Width;
						height = bmp.Height;
					}
					else {
						// Fit to size
						width = size;
						height = size;
					}
				}

				gr.DrawImage(bmp, result.Width / 2 - width / 2, result.Height / 2 - height / 2, width, height);

				if(Request["Big"] == null) {
					GraphicsPath path = new GraphicsPath();
					path.AddString(bmp.Width.ToString() + "x" + bmp.Height.ToString() + "/" + FormatSize(fi.Length), new FontFamily("Verdana"), 0, 9F, new Point(1, 1), StringFormat.GenericTypographic);
					Pen p = new Pen(Brushes.White, 2F);
					gr.DrawPath(p, path);
					gr.FillPath(Brushes.Black, path);

					/*Font f;
					if(Application["Font"] == null) {
						Application["Font"] = new Font("Verdana", 9F, FontStyle.Regular, GraphicsUnit.Pixel);
					}
					f = (Font)Application["Font"];
					gr.DrawString(bmp.Width.ToString() + "x" + bmp.Height.ToString() + "/" + FormatSize(fi.Length), f, Brushes.Black, new PointF(1, 1));*/
				}

				gr.Dispose();
				bmp.Dispose();

				EncoderParameters eps = new EncoderParameters(1);
				eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
				ImageCodecInfo ici = GetEncoderInfo("image/jpeg");

				result.Save(Settings.TempDirectory + thumbFile, ici, eps);
				result.Dispose();

			}

			Response.Clear();
			Response.AddHeader("content-type", "image/jpeg");
			Response.WriteFile(Settings.TempDirectory + thumbFile);

		}

		private string FormatSize(long size) {
			float res = size;
			if(size < 102400) { // Up to 100KB
				res = size / 1024F;
				return res.ToString("N1") + "KB";
			}
			else {
				res = size / 1048576F;
				return res.ToString("N1") + "MB";
			}
		}

		private ImageCodecInfo GetEncoderInfo(String mimeType) {
			ImageCodecInfo[] encoders;
			encoders = ImageCodecInfo.GetImageEncoders();
			for(int i = 0; i < encoders.Length; i++) {
				if(encoders[i].MimeType == mimeType)
					return encoders[i];
			}
			return null;
		}
</script>